
var em_hdrinmgr;

CHDrinMgr = function() {
	
	this.class_HDrinMgr = 1;
	this.objName = 'CHDrinMgr: ';
	
	this.param = {};
	this.started = false;
	this.lasttime = new Date(0);
	// store this ref 
	em_hdrinmgr = this;
	this.results = [ ] ;
	

	var self = this;
	em.cbs.userchanged.add(function() {self.reset();});
	
			// add global event cb 
	em.cbs.forcestop.add ( function() {self.force_stop = true; debug.log(self,'Set ForceStop ::' , self.force_stop);  });
};


/*
CHDrinMgr.prototype = new CHDrin();
CHDrinMgr.prototype.constructor = CHDrinMgr;
*/

CHDrinMgr.INVALID = 9;


CHDrinMgr.prototype = {

	reset : function() {

		this.started = false;
		this.results = [ ] ;

	 },

/*
	// chceck refresh time..
	// true : if any one is avaliable   
	bPossibleTime: function() {
		var i;
		for (i=0; i < this.results.length; i++ ) {
			if ( this.results[i].refresh <= 0 )
				return true;
		}
		return false;		
	},*/

	
	// move cstl.. in turn , h drin
/*
	startWave: function() {
		this.dfd_wholedone =  $.Deferred();
		
		this.doHDrin();
			// this.dfd_wholedone.reject();
		return this.dfd_wholedone.promise();
	},*/

/*
	// I F  
	start: function() {
		this.action.start();
	},*/

	
/*
	// log for cstl wave 
	cstl_logcb: function(msg) {
		em.util.print_result(msg);
	} ,*/

	 
/*
	registCB: function(cbs) {
		$.extend ( this.param, cbs);
		// this.registerCstlWaveCB(cbs);
		em.cstlmgr.registerCstlWaveCB(cbs);
	},*/

	
/*
	startRefreshTimer: function(){
		var self = this;
		// 10s ... 
		this.got_hdrinret = setInterval( function(){
			var i;
			for (i=0; i < self.results.length; i++ ) {
				self.results[i].refresh = (self.results[i].refresh <= 0 ) ? 0: self.results[i].refresh - 60
				debug.log(this, 'this', this, 'Hdrin refresh timer :', self.results[i].refresh);
			}
		}, 60 * 1000) ;		
	},
	
*/
	// func after move cstl 
	afterMove: function(data) {
		debug.log( this, ' After move in Hdrn');
		// get next citinum 
		var param = { citnum: em.cstlmgr.curCstlID(), hdrin_cb: this.hdrin_cb} ;
		this.hdrin.start(param);
		
	},
	
	// entry func 
	doHDrin: function () {
		debug.log(this, retName(this) , ' test go ');

		this.dfd_wholedone =  $.Deferred();

		// register  cstl move cb 
		var self = this;
		
		this.hdrin = new CHDrin();
		// if ( !this.hdrin_cb ) this.hdrin_cb  = $.Callbacks();
		this.hdrin_cb  = this.hdrin_cb  ||  $.Callbacks();
		
		
		// add each cstl result. cb .. 
		if (! this.got_hdrinret) {
			
			this.hdrin_cb.remove(this.got_hdrinret);
			
			// e  = { type : xxx, data : data...  }
			this.hdrin_cb.add( this.got_hdrinret = function (e) {
				
				if ( ! e) return;
				
				switch ( e.type ) {
					case 'GOT_HDRIN': 
						var hdrin_data = e.data;
						debug.log(this, ' got hdrin_cb', hdrin_data);
						// 	"code":0,"ret":
						//	{"gold":24148,"price":5135,"refresh":3600,"hero":{"gid":117,"p":11,"i":21,"c1":15,"g":2,"c2":295},"rumor":0}
						// {"code":0,"ret":{"price":372,"type":2,"refresh":2274}})
						
						if ( hdrin_data.ret.rumor ) {
							// got rumor 
							debug.log(this, ' got rumor !!!', hdrin_data.ret );
						}

						if ( hdrin_data.code == 1304 ) {
							debug.log(this, ' No Space for Hro !!!');
						}

						var cstl_idx = em.cstlmgr.getCurCstlIdx();
						if( ! self.results[cstl_idx] )  self.results[cstl_idx] = {};
						 						
						$.extend( self.results[ cstl_idx ] , hdrin_data); 	
						self.results[ cstl_idx ].nexttime = new Date($.now() + hdrin_data.ret.refresh * 1000);
					
						em.cstlview.updateHDrin(cstl_idx, hdrin_data);
					
					break;
					
					case 'FIN_CSTL':
						em.cstlmgr.moveNextCstl();
					break;
					
					case 'ERROR':
						em.util.print_result("Error !!! Hdrin :" , e.data) ;
					break; 
					
					case 'NOTY':
						debug.log( this, 1, 'hdrin: noty ', e.data);
					break; 
					
				}
			});
		}
		
		var outer = this;
	/*
		var hdrin_cbs = {
					success_cb : function(msg) {
						// outer.param.notify_cb(10000, msg);
						debug.log(this, " hdrin mgr :: success CB is called !!! ", msg) ;
						
						if(self.force_stop ) {
							debug.log(outer , " hdrin mgr :: forced stopped  ~!!! ") ;
							return;
						}
					
						em.cstlmgr.moveNextCstl();
					}, 
					error_cb : function(errorcode) {
						debug_log("Erro CB is called !!! in test case :" + errorcode) ;
						em.util.print_result("Erro CB is called !!! in test case :" + errorcode) ;
					},
					
					notify_cb : function(towhere, msg) {
						debug.log( this, 1, 'to W;  ' + towhere + '  :' + outer.cstl_idx + ' msg: ' + msg);
						
						// UI view 
						if ( towhere == 'ui') {
							// send result to UI Veiw
							// em.cstlview.updateHDrin(outer.cstl_idx, msg);
						} else {
							//default result box 
							em.util.print_result(msg);
						} 	  
						
					}, 
					
					hdrin_cb: hdrin_cb,
			};*/
	

		// start h drin
		// this.hdrin.registCB( hdrin_cbs );
		
		// go start with first ... <- after function : will start h drin 
		em.cbs.cstlmove1done.add( this.cb_cstlmove = function( ){ self.afterMove();}  );
		debug.log(this, this.cb_cstlmove);
		
		em.cbs.cstlwavedone.add( this.cb_wavedone = function() { self.completed();});	
		em.cstlmgr.moveCstlAt(0);

		return this.dfd_wholedone.promise();
	},

	// all done 
	completed: function(data) {
		console.log('cb called complted!!!!!!!');
		
		this.dfd_wholedone.resolve();
		// remove cbs 
		em.cbs.cstlmove1done.remove(this.cb_cstlmove);
		em.cbs.cstlwavedone.remove(this.cb_wavedone);
		
		// release newed obj
		delete	this.hdrin;
		
		this.started = true;
					
		// check rumor : cstl id ,  outer, cb func 
		em.tasks.getRumor( em.cstlmgr.curCstlID() , this, this.gotRumor );		// after complete cstl..  -1  �꾩슂. ! 
	},

	// got task result for rumor 
	gotRumor: function(data) {
		// update rumor info
		debug.log(this, 1, data	) ;
		em.infoview.updateRumor(data);
	},



/*
	restart: function(timeout) {
		
		this.retry_timer = setTimeout( function(){
			em.hdrinmgr.doHDrin();
			}, timeout) ;	
				 
	},*/

	
/*
	getNextTime: function() {
		return this.lasttime;
	},
	*/

	
	/// ret next hdrin check time 
	// ret : min time..   to check every time.. of any cstl is avalable 
	getNextCheckTime: function() {

		// when no one is valid : start right now 
		if ( this.results.length == 0 ) {
			return -1;
		} 
				
		// filter only avail  : except no space.. 
		//  data.code == 1304  // no space 
		this.results.filter(function(el){
			return (el.code == 0 && el.refresh > 0 );		// only valid 
		});
		
		// sort refresh time   :acendent 
		this.results.sort(function(a,b) {
			return a.refresh - b.refresh;
		});
		
		// return first refresh time.
		return this.results[0].nexttime;    
	},

};

 

 
